package hfst;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import static org.junit.Assert.*;

import org.junit.Test;

import affd.hfst.TransducerAlphabet;
import affd.hfst.TransducerHeader;
import affd.hfst.TransducerWeighted;

public class TransducerWeightedTest {


	private TransducerWeighted setUp() {
		String transducerFileName = "mor-omorfi.hwfst.ol";
    	FileInputStream transducerfile = null;
    	TransducerWeighted transducer = null;	
       	try { 
    		transducerfile = new FileInputStream(transducerFileName); 
	    	TransducerHeader h = new TransducerHeader(transducerfile);
	    	DataInputStream charstream = new DataInputStream(transducerfile);
	    	TransducerAlphabet a = new TransducerAlphabet(charstream, h.getSymbolCount());
  
	    	transducer = new TransducerWeighted(transducerfile, h, a); 
       	}
    	catch (IOException e) {
    		fail("File not found: couldn't read transducer file " + transducerFileName + ".");
    	}
    	return transducer;
	}
	
	@Test
	public void testWasPronoun() {
		
		TransducerWeighted transducer = setUp();
		
		String word = "minun";
		
		assertTrue(transducer.analyze(word));
		transducer.printAnalyses();
		assertTrue(transducer.wasPronoun());

	}

	@Test
	public void testGetCaseAndNumber() {
		TransducerWeighted transducer = setUp();
		String caseAndNumber;
		String word1 = "auto";
		String wordInPlural = "autoja";
		String word2 = "autokorjaamo";
		String wordInSingle = "autokorjaamossa";
		
		assertTrue(transducer.analyze(wordInPlural));
		caseAndNumber = transducer.getCaseAndNumber(word1, wordInPlural);
		assertEquals("PAR;PL", caseAndNumber);
		
		assertTrue(transducer.analyze(wordInSingle));
		caseAndNumber = transducer.getCaseAndNumber(word2, wordInSingle);
		assertEquals("INE;SG", caseAndNumber);
	}
	
	@Test
	public void testGetCaseAndNumberOfUnexistedWord() {
		TransducerWeighted transducer = setUp();
		String caseAndNumber;
		String word1 = "asldfh";
		String wordInSomeForm = "alsdfhas";

		
		assertTrue(transducer.analyze(wordInSomeForm));
		caseAndNumber = transducer.getCaseAndNumber(word1, wordInSomeForm);
		assertNull(caseAndNumber);
	}
	
	@Test
	public void testGetCaseAndNumberWithNoCase() {
		TransducerWeighted transducer = setUp();
		String caseAndNumber;
		String word1 = "olla";
		String wordInSomeForm = "olen";

		
		assertTrue(transducer.analyze(wordInSomeForm));
		caseAndNumber = transducer.getCaseAndNumber(word1, wordInSomeForm);
		assertNull(caseAndNumber);
	}
	
	@Test
	public void testPrintAnalyses1() {
		TransducerWeighted transducer = setUp();
       	ArrayList<String> basicForms;
       	
       	String word = "kukkia";
       	
       	assertTrue(transducer.analyze(word));
    	basicForms = transducer.printAnalyses(); 		
	    assertEquals("kukka", basicForms.get(0));
	    assertEquals("kukkia", basicForms.get(1));
	    assertEquals(2, basicForms.size());
		
	}
	
	@Test
	public void testPrintAnalyses_same_forms_dont_added() {
		TransducerWeighted transducer = setUp();
       	ArrayList<String> basicForms;
       	
       	String word = "Tämän";
       	
    	assertTrue(transducer.analyze(word));
    	basicForms = transducer.printAnalyses(); 		
	    assertEquals("tämä", basicForms.get(0));
	    assertEquals(1, basicForms.size());
	 			
	}

}



















